# -*- coding: utf-8 -*-

import json
from public_util.util.AutoUrl import Route
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from package.models import Project, Branch, Channel, ChannelPackage


@Route()
def channel(request):
    """渠道CRUD接口"""
    if request.method == "GET":
        req = request.REQUEST.get
        init = req("init", 0)
        if init:
            pro_objs = Project.filter_project(request)
            return render_to_response("channel/channel_index.html", locals())
        else:
            objs = Channel.filter_record(request)
            item_list = []
            for obj in objs:
                item_list.append([obj.project.pro_name, obj.sdk_name, obj.get_plat_display(),
                                  obj.plugin_name, obj.create_time.strftime("%Y-%m-%d %H:%M:%S"), obj.id])
            return HttpResponse(json.dumps({"aaData": item_list}), content_type="application/json")
    elif request.method == "POST":
        err_msg = ""
        params = request.post_dic
        params["project"] = Project.objects.filter(id=params.get("project", 0)).first()
        _id = request.GET.get("id", 0)
        if _id:
            try:
                Channel.objects.filter(id=_id).update(**params)
            except Exception as e:
                err_msg = e
        else:
            try:
                Channel(**params).save()
            except Exception as e:
                err_msg = e
        return render_to_response("feedback.html", locals())


@Route()
def channel_detail(request):
    """渠道详情页面接口"""
    plat = Channel.MOBILE_OS_TYPE
    pro_tuple = Project.filter_project(request).values_list("id", "pro_name")
    _id = request.REQUEST.get("id", 0)
    if _id:
        model = Channel.objects.filter(id=_id).first()
    return render_to_response("channel/channel_detail.html", locals())


@Route()
def channel_remark(request):
    """渠道备注CRUD接口"""
    if request.method == "GET":
        _id = request.REQUEST.get("id", 0)
        model = Channel.objects.filter(id=_id).first()
        return render_to_response("channel/channel_remark.html", locals())
    elif request.method == "POST":
        err_msg = ""
        _id = request.GET.get("id", 0)
        remark = request.POST.get("remark", "")
        if not all([_id, remark]):
            err_msg = "参数缺失！"
            return render_to_response("feedback.html", locals())
        try:
            Channel.objects.filter(id=_id).update(remark=remark)
        except Exception as e:
            err_msg = e
        return render_to_response("feedback.html", locals())


@Route()
def channel_package(request):
    """渠道任务CRUD接口"""
    req = request.REQUEST.get
    init = req("init", 0)
    if init:
        pro_objs = Project.filter_project(request)
        bra_objs = Branch.filter_project(request)
        return render_to_response("channel/channel_package_index.html", locals())
    else:
        objs = ChannelPackage.filter_record(request)
        item_list = []
        for obj in objs:
            item_list.append([obj.project.pro_name, obj.branch.branch_name, obj.channel.sdk_name,
                              obj.channel.get_plat_display(), obj.param_folder,
                              '<a href="%s" download="%s">%s</a>' % (obj.file_link, obj.file_name, obj.file_name),
                              obj.version, obj.create_time.strftime("%Y-%m-%d %H:%M:%S")])
        return HttpResponse(json.dumps({"aaData": item_list}), content_type="application/json")
